#! /bin/sh
# $Id: test-runtime-config 304 2006-09-16 21:28:26Z sfsetse $
#
# Author: Kate Ward <kate.ward@forestent.com>
#

# find myself
whoAmI=`basename $0`
whereAmI=`dirname $0`
whereAmI=`cd "${whereAmI}" 2>/dev/null && pwd || echo "${whereAmI}"`

#-----------------------------------------------------------------------------
# functions
#

test_cleanup()
{
  echo '### test script cleanup function called###'
}

loadLog4sh()
{
  config="$1"

  if [ -r log4sh ]; then
    LOG4SH_CONFIGURATION="${config}" . ./log4sh
  else
    echo "ERROR: could not load (log4sh)" >&2
    exit 1
  fi
  logger_trace "whoAmI=${whoAmI} whereAmI=${whereAmI}"
}

#-----------------------------------------------------------------------------
# pre-configure log4sh
#
# the functions called in this section are meant to exactly reproduce what the
# log4sh.properties does so that the common tests that are run generate the
# same output.
#

configLog4sh()
{
  # reset the log4sh configuration
  log4sh_resetConfiguration

  # setup the default logging level to INFO
  logger_setLevel INFO

  # add a file appender at the default level that logs to STDERR
  logger_addAppender mySTDERR
  appender_setType mySTDERR FileAppender
  appender_file_setFile mySTDERR STDERR
  appender_activateOptions mySTDERR

  # add a file appender at the DEBUG level with the default layout
  logger_addAppender mySimple
  appender_setLevel mySimple DEBUG
  appender_setType mySimple FileAppender
  appender_file_setFile mySimple log4sh-simple.log
  appender_activateOptions mySimple

  # add file appender at the default level with a Pattern layout
  logger_addAppender myPattern
  appender_setType myPattern RollingFileAppender
  appender_file_setFile myPattern log4sh-pattern.log
  appender_setLayout myPattern PatternLayout
  appender_setPattern myPattern '%d [%p] (%F) - %m%n'
  appender_activateOptions myPattern

  # add a syslog appender at the default level with a facility of local4
  logger_addAppenderWithPattern mySyslog '[%p] (%F) - %m%n'
  appender_setType mySyslog SyslogAppender
  appender_syslog_setFacility mySyslog local4
  appender_activateOptions mySyslog
}

#-----------------------------------------------------------------------------
# main
#

# setup trap handler.  must be done before loading log4sh
trap 'test_cleanup' EXIT

# load and configure log4sh
loadLog4sh none
configLog4sh

# load and run the common tests
commonTests='test-common'
if [ -r "${commonTests}" ]; then
  . ${whereAmI}/${commonTests}
else
  logger_fatal "could not load the common tests"
  exit 1
fi
